package com.nice.demo.test;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;

import java.util.Random;


public class HandWritingCanvas {

    private String TAG = "HandWritingCanvas";
    private Random random = new Random();
    private RectF dirtyRect = new RectF();
    private int tileWidth = 200;
    public int width;
    public int height;
    private int tileHorizontalCount;
    private int tileVerticalCount;
    private HandWritingCanvas.Tile[] tiles;

    public int randomColor() {
        return Color.argb(255, this.random.nextInt(255), this.random.nextInt(255), this.random.nextInt(255));
    }

    public HandWritingCanvas(int width, int height) {
        this.width = width;
        this.height = height;
        this.tileHorizontalCount = width / this.tileWidth + (width % this.tileWidth == 0 ? 0 : 1);
        this.tileVerticalCount = height / this.tileWidth + (height % this.tileWidth == 0 ? 0 : 1);
        if (HandWritingViewHelper.DEBUG) {
            XHLog.i(this.TAG, "width = " + width + ", height = " + height + ", tileHorizontalCount = " + this.tileHorizontalCount + ", tileVerticalCount = " + this.tileVerticalCount, new Object[0]);
        }

        this.tiles = new HandWritingCanvas.Tile[this.tileHorizontalCount * this.tileVerticalCount];
        int i = 0;

        for(int x = 0; x < this.tileHorizontalCount; ++x) {
            for(int y = 0; y < this.tileVerticalCount; ++y) {
                Bitmap bitmap = Bitmap.createBitmap(this.tileWidth, this.tileWidth, Bitmap.Config.ARGB_4444);
                HandWritingCanvas.Tile tile = new HandWritingCanvas.Tile(bitmap, x, y);
                this.tiles[i] = tile;
                ++i;
            }
        }

    }

    public void drawPath(Path path, Paint paint, float penSize) {
        path.computeBounds(this.dirtyRect, false);
        HandWritingCanvas.Tile[] var4 = this.tiles;
        int var5 = var4.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            HandWritingCanvas.Tile tile = var4[var6];
            boolean inside = tile.rect.intersects((int)this.dirtyRect.left, (int)this.dirtyRect.top, (int)this.dirtyRect.right, (int)this.dirtyRect.bottom);
            if (inside) {
                paint.setStrokeWidth(penSize);
                this.drawPath(path, tile.canvas, paint);
            }
        }

    }

    private void drawPath(Path path, Canvas canvas, Paint paint) {
        canvas.drawPath(path, paint);
    }

    public void drawLine(float startX, float startY, float endX, float endY, Paint paint) {
        this.dirtyRect.left = startX > endX ? endX : startX;
        this.dirtyRect.right = startX < endX ? endX : startX;
        this.dirtyRect.top = startY > endY ? endY : startY;
        this.dirtyRect.bottom = startY < endY ? endY : startY;
        HandWritingCanvas.Tile[] var6 = this.tiles;
        int var7 = var6.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            HandWritingCanvas.Tile tile = var6[var8];
            boolean inside = tile.rect.intersects((int)this.dirtyRect.left, (int)this.dirtyRect.top, (int)this.dirtyRect.right, (int)this.dirtyRect.bottom);
            if (inside) {
                tile.canvas.drawLine(startX, startY, endX, endY, paint);
            }
        }

    }

    public void drawCanvas(Canvas canvas, Paint paint) {
        HandWritingCanvas.Tile[] var3 = this.tiles;
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            HandWritingCanvas.Tile tile = var3[var5];
            canvas.drawBitmap(tile.bitmap, (float)tile.rect.left, (float)tile.rect.top, paint);
        }

    }

    public void drawRect(Rect rect, Paint paint) {
        HandWritingCanvas.Tile[] var3 = this.tiles;
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            HandWritingCanvas.Tile tile = var3[var5];
            boolean inside = tile.rect.intersects(rect.left, rect.top, rect.right, rect.bottom);
            if (inside) {
                tile.canvas.drawRect(rect, paint);
            }
        }

    }

    public void clearCanvas() {
        HandWritingCanvas.Tile[] var1 = this.tiles;
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            HandWritingCanvas.Tile tile = var1[var3];
            tile.bitmap.eraseColor(0);
        }

    }

    public void releaseCanvas() {
        HandWritingCanvas.Tile[] var1 = this.tiles;
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            HandWritingCanvas.Tile tile = var1[var3];
            tile.bitmap.recycle();
        }

    }

    public void drawBitmap(Bitmap bitmap, Paint paint) {
        HandWritingCanvas.Tile[] var3 = this.tiles;
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            HandWritingCanvas.Tile tile = var3[var5];
            if (bitmap.getWidth() > tile.rect.left && bitmap.getHeight() > tile.rect.top) {
                tile.canvas.drawBitmap(bitmap, tile.rect, tile.rect, paint);
            }
        }

    }

    class Tile {
        Bitmap bitmap;
        Canvas canvas;
        Rect rect;
        int index = 0;
        int x;
        int y;

        public Tile(Bitmap bitmap, int x, int y) {
            this.bitmap = bitmap;
            this.canvas = new Canvas(bitmap);
            this.canvas.translate((float)(-x * HandWritingCanvas.this.tileWidth), (float)(-y * HandWritingCanvas.this.tileWidth));
            this.canvas.setDrawFilter(new PaintFlagsDrawFilter(0, 7));
            this.x = x;
            this.y = y;
            int left = x * HandWritingCanvas.this.tileWidth;
            int top = y * HandWritingCanvas.this.tileWidth;
            this.rect = new Rect(left, top, left + HandWritingCanvas.this.tileWidth, top + HandWritingCanvas.this.tileWidth);
            this.index = y * HandWritingCanvas.this.tileHorizontalCount + x;
        }
    }
}
